package xm.manager
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	import xm.definition.Direction;
	import xm.definition.TransitionStatus;
	import xm.definition.TransitionType;
	
	/**
	 * <br> 클래스 설명 : 화면전환 관리자 </br>
	 * @author 김정환
	 */	
	public class TransitionMananger
	{
		
		
		//-----------------------------------------------------------------------------------
		// singleTon Pattern
		//-----------------------------------------------------------------------------------
		private static var _instance : TransitionMananger;
		
		/**
		 * @return TransitionMananger
		 */		
		public static function getInstance():TransitionMananger
		{
			if( _instance == null ) _instance = new TransitionMananger( new SingletonEnforce() );
			return _instance;
		}
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _dicBitmap : Dictionary = new Dictionary( true );
		private var _dicComplete : Dictionary = new Dictionary( true );
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function TransitionMananger( se : SingletonEnforce )
		{
			if( se == null ) throw new Error( "TransitionMananger 는 생성하여 사용할 수 없습니다." );
		}
		
		
		/**
		 * <br>트랜지션 명령</br>
		 * @param target : DisplayObject - 트랜지션 대상
		 * @param transitionStatus : String - TransitionStatus.IN, TransitionStatus.OUT
		 * @param transitionType : String - TransitionType.SLIDE ....
		 * @param direction : String - Direction.RIGHT, Direction.LEFT
		 * @param onComplete : Function - 콜백함수.
		 */		
		public function playTransition( target:DisplayObject, transitionStatus:String, transitionType:String, direction:String, onComplete:Function=null ):void
		{
			
			var bd : BitmapData = new BitmapData( target.width, target.height );
			bd.draw( target );
			
			_dicBitmap[ target ] = new Bitmap( bd );
			_dicComplete[ target ] = onComplete;
			
			
			switch( transitionType )
			{
				
			case TransitionType.SLIDE :
				slide( target, transitionStatus, direction );
				break;
				
			}
			
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		/*
			- 좌우로 슬라이드되는 트랜지션
		*/
		private function slide( target:DisplayObject, transitionStatus:String, direction:String ):void
		{
			var bm : Bitmap = _dicBitmap[ target ] as Bitmap,
				destination : Number = 0;
			
			StageManager.getInstance().stage.addChild( bm );
			
			if( transitionStatus == TransitionStatus.IN )
			{
				if( direction == Direction.RIGHT )
				{
					bm.x = -bm.width;
					destination = 0;
				}
				else if( direction == Direction.LEFT )
				{
					bm.x = StageManager.getInstance().stage.stageWidth;
					destination = 0;
				}
			}
			else if( transitionStatus == TransitionStatus.OUT )
			{
				if( direction == Direction.RIGHT )
				{
					bm.x = target.x;
					destination = StageManager.getInstance().stage.stageWidth;
				}
				else if( direction == Direction.LEFT )
				{
					bm.x = target.x;
					destination = -bm.width;
				}
			}
			
			
			TweenLite.to( bm, 1, {x:destination, onComplete:transitionPlayComplete, onCompleteParams:[target]} );
		}
		
		
		
		
		// transition 이 모두 끝났을 때 callback 함수 실행하고 dictionary에 담긴 변수 제거.
		private function transitionPlayComplete( target:DisplayObject ):void
		{
			if( _dicComplete[ target ] ) _dicComplete[ target ]();
			
			delete _dicBitmap[ target ];
			delete _dicComplete[ target ];
		}
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
	}
}






class SingletonEnforce{}